<?php

namespace app\common\model;

use think\Model;

class Admin extends Model
{
    //
    public function add_one($info){
        $ins['name'] = $info['name'];
        $ins['password'] = md5($info['password']);
        return $this->insert($ins);
    }

    public function get_one($id,$field='*'){
        return $this->field($field)->where(['id'=>$id])->find();
    }
    public function get_name_one($name,$field='*'){
        return $this->field($field)->where(['name'=>$name])->find();
    }

    public function edit($id,$info){
        $ins['name'] = $info['name'];
        $data = $this->where(['id'=>$id])->find();
        if($data['password'] != $info['password']){
            $ins['password'] = md5($info['password']);
        }
        return $this->where(['id'=>$id])->update($ins);
    }

    public function del($id){
        return $this->where(['id'=>$id])->delete();
    }

    static function getList($page,$pageSize,$search,$field=['id','name','password']){
        $currentPage = $page;
        $page = ($page - 1) * $pageSize;
        $info = self::field($field)->limit($page,$pageSize)->select();
        $count = self::count();
        $arr =  ['list'=>$info,'count'=>$count,'page'=>$currentPage,'pageSize'=>$pageSize];
        return $arr;
    }

}
